//
//  Consume_GD_TopTableViewCell.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/9/4.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "Consume_GD_TopTableViewCell.h"

@implementation Consume_GD_TopTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}
-(void)initialize{
    
    _iconImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero imageNamesGroup:@[@"Bitmap",@"Bitmap",@"Bitmap"]];
    _iconImageView.currentPageDotColor = ThemeMaoColor;
    _iconImageView.autoScroll = NO; 
    
    [self addSubview:_iconImageView];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(self.iconImageView.mas_width);
    }];
    
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.text = @"Apple iPhone X 64GB 国行正品 移动联通电信4G手机赠送移动电源";
    _nameLabel.textColor = HexColor(0x454545);
    _nameLabel.font = UIFontBoldCustom(15);
    _nameLabel.numberOfLines = 0;
    CGSize size = [self boundingRectWithSize:@"Apple iPhone X 64GB 国行正品 移动联通电信4G手机赠送移动电源" Font:UIFontBoldCustom(15) Size:CGSizeMake(ScreenWidthMao-30, 16)];
    [self addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(5);
        make.height.mas_offset(@(size.height+15));
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = ThemeMaoLineColor;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10);
        make.height.mas_offset(@0.5);
    }];
    
    _instalmentLabel = [[PriceView alloc]init];
    [_instalmentLabel updateInfoPrice:@"7599.00" periods:@"" priceShowType:PriceShowTypeGoodsDetail];
    [self addSubview:_instalmentLabel];
    [_instalmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(line.mas_bottom).offset(10);
        make.width.mas_offset(@120);
        make.height.mas_offset(@20);
    }];
    
    _configurationView = [[UIView alloc]init];
    _configurationView.backgroundColor = [UIColor whiteColor];
    [self creatLabel:2 view:self.configurationView array:@[@"支持积分抵现",@"不支持7天退货"]];
    [self addSubview:_configurationView];
    [_configurationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.instalmentLabel.mas_left);
        make.right.mas_equalTo(self);
        make.top.mas_equalTo(self.instalmentLabel.mas_bottom).offset(10);
        make.height.mas_offset(@14);
    }];
    
//    _priceLabel = [[UILabel alloc]init];
//    _priceLabel.textColor = ThemeMaoBlackColor;
//    _priceLabel.text = @"总价：7599";
//    _priceLabel.font = [UIFont systemFontOfSize:12];
//    [self addSubview:_priceLabel];
//    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.nameLabel);
//        make.top.mas_equalTo(self.instalmentLabel.mas_bottom).offset(10);
//        make.right.mas_equalTo(self.nameLabel);
//
//    }];
    
}

-(void)creatLabel:(NSInteger)count view:(UIView *)view array:(NSArray *)array{
    
    
    CGFloat currentY = 0;
    CGFloat currentX = 0;
    
    
    CGFloat forwardHeight = 0;
    //间距
    CGFloat labelSpace = 3;
    //字体大小
    CGFloat fontFloat = 11;
    
    for (int i=0; i<count; i++) {
        NSString *title = array[i];
        UILabel *label = [[UILabel alloc]init];
        label.text = title;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = HexColor(0x999999);
        label.layer.masksToBounds = YES;
        label.layer.borderColor = HexColor(0xCCCCCC).CGColor;
        if (i == 0) {
            label.layer.borderColor = ThemeMaoRedColor.CGColor;
            label.textColor = ThemeMaoRedColor;
        }
        label.layer.borderWidth = .5f;
        label.layer.cornerRadius = 2;
        label.font = [UIFont systemFontOfSize:fontFloat];
        [view addSubview:label];
        
        CGSize size = [self boundingRectWithSize:array[array.count -count + i] Font:[UIFont systemFontOfSize:fontFloat] Size:CGSizeMake(ScreenWidthMao - 40, 40)];
        if (currentX + size.width >ScreenWidthMao - 10) {
            currentX = labelSpace;
            currentY = currentY + forwardHeight + 10;
        }
        label.frame = CGRectMake(currentX, currentY, size.width, 15);
        [view addSubview:label];
        forwardHeight = 25;
        currentX = currentX + size.width + 8;
        
    }
}

- (CGSize) boundingRectWithSize:(NSString*)string Font:(UIFont*) font Size:(CGSize) size{
    CGSize _size;
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    NSDictionary *attribute = @{NSFontAttributeName: font};
    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
    NSStringDrawingUsesLineFragmentOrigin |
    NSStringDrawingUsesFontLeading;
    _size = [string boundingRectWithSize:size options: options attributes:attribute context:nil].size;
#else
    _size = [string sizeWithFont:font constrainedToSize:size];
#endif
    _size.height += 10;
    _size.width += 10;
    return _size;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
